package com.cdream.basic.redis.lettuce;

import com.cdream.basic.redis.config.RedisConst;
import com.cdream.basic.redis.config.model.RedisTestModel;
import io.lettuce.core.*;
import io.lettuce.core.api.StatefulRedisConnection;
import io.lettuce.core.api.sync.RedisCommands;
import io.lettuce.core.resource.DefaultClientResources;
import lombok.extern.slf4j.Slf4j;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Date;
import java.util.List;

@Slf4j
public class RedisSentinelClientTest {
    public static void main(String[] args) throws IOException {


        RedisURI uri = RedisURI.builder()
                .withSentinel(RedisConst.HOST,RedisConst.SENTINEL_PORT)
                .withSentinelMasterId("mymaster")
                .withDatabase(0)
                .build();

        DefaultClientResources resources = DefaultClientResources.builder()
                .ioThreadPoolSize(Runtime.getRuntime().availableProcessors())
                .computationThreadPoolSize(Runtime.getRuntime().availableProcessors())
                .build();

        RedisClient client = RedisClient.create(resources, uri);
        ClientOptions options = ClientOptions.builder()
                .autoReconnect(true)
                .pingBeforeActivateConnection(true)
                .build();
        client.setOptions(options);
        StatefulRedisConnection<String, String> connect = client.connect();
        RedisCommands<String, String> cmd = connect.sync();
//        Double dist = cmd.geodist("cities:locations", "beijing", "tianjin", GeoArgs.Unit.km);
        //language=Lua

        String luaScript = "local dog = redis.call(\"get\", \"dog\")\n" +
                "local dio = redis.call(\"get\", \"dio\")\n" +
                "local res = redis.call(\"get\", KEYS[1])\n" +
                "return { dog, dio,res }";
        List<Object> eval = cmd.eval(luaScript, ScriptOutputType.MULTI, "num");
        log.info("lua脚本执行结果：{}", eval);
        List<String> mylist = cmd.lrange("dogs", 0, -1);
        log.info("列表数据：{}", mylist);

//        RedisTestModel model = new RedisTestModel();
//        model.setAge(12);
//        model.setName("狗子");
//        model.setDate(new Date());

//        byte[] serialize = ProtoStuffUtil.serialize(model);
//        String m = cmd.set("model", new String(serialize, StandardCharsets.UTF_8));
//        RedisTestModel redisModel = ProtoStuffUtil.deserialize(cmd.get("model").getBytes(StandardCharsets.UTF_8), model.getClass());
//        log.info(JSONUtil.toJsonStr(ProtoStuffUtil.deserialize(serialize,RedisTestModel.class)));
//        log.info("restore返回的值为：{},取出的值：{}",m,redisModel);

//        RedisAsyncCommands<String, String> async = connect.async();
//
//        async.setAutoFlushCommands(false);
//        async.set("dog","1");
//        async.set("pig","2");
//        async.flushCommands();
        cmd.clientSetname("lettuce");
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String s;
        while ((s = br.readLine()) != null) {
            if ("quit".equals(s)) {
                break;
            }
            System.out.println(s);
        }
        connect.close();
    }
}
